const express=require("express");
const router=express.Router();
const pool=require("../pool");
//非空验证中间件
router.use("/",(req,res,next)=>{
    var obj=req.body;
    var n=400;
    for(var k in obj){
        n++;
        if(!obj[k]){
            res.send({code:n,msg:k+"不能为空"});
            return;
        }
    }
    next();
})
//用户注册
router.post("/reg",(req,res,next)=>{
    var obj=req.body;
    if(!/^1[3-9]\d{9}$/.test(obj.phone_number)){
        res.send({code:411,msg:"手机号格式不正确"});
        return;
    }
    pool.query("INSERT INTO users SET ?",[obj],(err,result)=>{
        if(err){
            //把错误交给下一个中间件
            next(err);
            //阻止往后执行
            return;
        }
        res.send({code:200,msg:"注册成功"});
    })
});
//用户登录
router.post("/login",(req,res,next)=>{
    var obj=req.body;
    pool.query("SELECT * FROM users WHERE uname=? AND upwd=?",[obj.uname,obj.upwd],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length){
            res.send({code:200,msg:"登录成功"});
        }else{
            res.send({code:201,msg:"用户名或密码不正确"});
        }
    })
})
//修改用户
router.put("/",(req,res,next)=>{
    var obj=req.body;
    pool.query("UPDATE users SET ? WHERE uid=?",[obj,obj.uid],(err,result)=>{
        if(err){
            next(err);
            return;
        };
        if(result.affectedRows>0){
            res.send({code:200,msg:"修改成功"})
        }else{
            res.send({code:201,msg:"修改失败"})
        }
    })
});
//删除用户
router.delete("/:uid",(req,res,next)=>{
    pool.query("DELETE FROM users WHERE uid=?",[req.params.uid],(err,result)=>{
        if(err){
            next(err);
            return;
        };
        if(result.affectedRows>0){
            res.send({code:200,msg:"删除成功"})
        }else{
            res.send({code:201,msg:"删除失败"})
        }
    })
});
//用户列表
router.get("/",(req,res,next)=>{
    var obj=req.query;
    if(!obj.pagination)obj.pagination=1;
    if(!obj.quantity)obj.quantity=5;
    console.log(obj);
    var size=Math.abs(parseInt(obj.quantity));
    pool.query("SELECT uid,uname,email,phone_number FROM users LIMIT ?,?",[(Math.abs(parseInt(obj.pagination))-1)*size,size],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        res.send({code:200,msg:"查询成功",data:result});
    });
})
//按照编号查找用户
router.get("/:uid",(req,res,next)=>{
    pool.query("SELECT uname,email,phone_number FROM users WHERE uid=?",[req.params.uid],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length>0){
            res.send({code:200,msg:"查找成功",data:result[0]})
        }else{
            res.send({code:201,msg:"查找失败"})
        }
    })
})
//检查用户名是否被占用
router.post("/uname/:uname",(req,res,next)=>{
    console.log(req.body);
    pool.query("SELECT uname FROM users WHERE uname=?",[req.params.uname],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length>0){
            res.send({code:201,msg:"该用户名已被占用"})
        }else{
            res.send({code:200,msg:"该用户名可以使用"})
        }
    })
})
//检查邮箱是否被占用
router.post("/email/:email",(req,res,next)=>{
    pool.query("SELECT email FROM users WHERE email=?",[req.params.email],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length>0){
            res.send({code:201,msg:"该邮箱已被占用"})
        }else{
            res.send({code:200,msg:"该邮箱可以使用"})
        }
    })
})
//检查手机号是否被占用
router.post("/phone_number/:phone_number",(req,res,next)=>{
    pool.query("SELECT phone_number FROM users WHERE phone_number=?",[req.params.phone_number],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length){
            res.send({code:201,msg:"该手机号码已被占用"})
        }else{
            res.send({code:200,msg:"该手机号码可以使用"})
        }
    })
})
module.exports=router;